package com.mcxx.modules.tool.service;

import com.mcxx.common.ExternalFileUtil;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.tool.service.impl.JinminExitService;
import com.mcxx.modules.tool.service.impl.JinminSubsistenceService;
import com.mcxx.modules.tool.service.impl.JinminTempService;
import com.mcxx.modules.base.service.read.AreaReadService;
import com.mcxx.modules.external.jinmin.service.read.JMBAF01ReadService;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayEntity;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayParam;
import com.mcxx.modules.system.dto.AreaMappingDTO;
import com.mcxx.modules.temporary.fundpay.entity.TmpFundPayEntity;
import com.mcxx.modules.temporary.fundpay.param.TmpFundPayParam;
import com.mcxx.result.BaseResult;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.DateUtil;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@Service
@TaTransactional
public class GenerateJinMinService {

    private final static org.slf4j.Logger logger = LoggerFactory.getLogger(GenerateJinMinService.class);
    private static String donePath = "/home/society/application/jinmin/jm_db_done.txt";
    private static String doneTmpPath = "/home/society/application/jinmin/jm_ls_done.txt";

    @Autowired
    private AreaReadService areaReadService;
    @Autowired
    private JMBAF01ReadService jmBAF01ReadService;
    @Autowired
    private JinminSubsistenceService jinminSubsistenceService;
    @Autowired
    private JinminTempService jinminTempService;
    @Autowired
    private JinminExitService jinminExitService;

    private ExecutorService executorService = Executors.newFixedThreadPool(50);

    
    public BaseResult<Boolean> jinminData(String strAreaCodes, String strMonths, String batchNo) {
        BaseResult<Boolean> result = new BaseResult<Boolean>();
        if(CommonUtil.invalidArgs(strMonths) || CommonUtil.invalidArgs(strAreaCodes) || CommonUtil.invalidArgs(batchNo)) {
            throw new BizException(FailureCode.ERR_10000);
        }

        String[] areaCodes = strAreaCodes.split(",");
        for(String areaCode : areaCodes){
            
            List<AreaMappingDTO> areaCodeBycCodeALevle = areaReadService.getAreaCodeBycCodeALevle(areaCode);
            if(CollectionUtils.isNotEmpty(areaCodeBycCodeALevle)){

                Date date = DateUtil.str2Date(strMonths+"01", DateUtil.sdf6);
                String format = DateUtil.sdf2.format(date);

                for(AreaMappingDTO dto : areaCodeBycCodeALevle){
                    String queryAreaCode = dto.getAreaCode().substring(0, 6);
                    
                    List<String> qb = jmBAF01ReadService.getOrderByJinmin(queryAreaCode, strMonths + "01");
                    if(CollectionUtils.isNotEmpty(qb)){
                        for(String id : qb){
                            try{
                                if(ExternalFileUtil.traverseFolder(donePath, id)){
                                    logger.info("oId 已处理");
                                    continue;
                                }else{
                                    executorService.execute(new Runnable() {
                                        @Override
                                        public void run() {
                                            jinminSubsistenceService.dibaotekunData(strMonths,id,batchNo);
                                        }
                                    });
                                }
                            }catch (Exception e){
                                logger.error(e.getMessage());
                            }
                        }
                    }
                    
                    List<String> qbtmp = jmBAF01ReadService.getTmpOrderByJinmin(queryAreaCode, strMonths + "01");
                    if(CollectionUtils.isNotEmpty(qbtmp)){
                        for(String id : qbtmp){
                            try{
                                if(ExternalFileUtil.traverseFolder(doneTmpPath, id)){
                                    logger.info("oId 已处理");
                                    continue;
                                }else{
                                    executorService.execute(new Runnable() {
                                        @Override
                                        public void run() {
                                            jinminTempService.linshiData(strMonths,id,batchNo);
                                        }
                                    });
                                }
                            }catch (Exception e){
                                logger.error(e.getMessage());
                            }
                        }
                    }
                    
                    String lastDate = DateUtil.reduceMonth(strMonths, 1);
                    List<String> qbtb = jmBAF01ReadService.getOrderByJinmin(queryAreaCode, lastDate + "01");
                    if(CollectionUtils.isNotEmpty(qbtb)){
                        for(String id : qbtb){
                            try{
                                if(ExternalFileUtil.traverseFolder(donePath, id)){
                                    logger.info("oId 已处理");
                                    continue;
                                }else{
                                    executorService.execute(new Runnable() {
                                        @Override
                                        public void run() {
                                            jinminExitService.dibaotekunData(lastDate,id,batchNo);
                                        }
                                    });
                                }
                            }catch (Exception e){
                                logger.error(e.getMessage());
                            }

                        }
                    }

                }
            }
        }
        return null;
    }
}
